﻿
using JobBack.Models;
using QG.JobBack.BLL.IService;
using QG.JobBack.BLL.Service;
using QG.JobBack.DAL;
using QH.JobBack.Model;
using System.Web.Mvc;

namespace JobBack.Controllers
{
    public class LoginController : BaseController
    {
        private IUserServices _userService;
        public LoginController(IUserServices userService)
        {
            _userService = userService;
        }
        /// <summary>
        /// 登录页
        /// </summary>
        /// <returns></returns>
        [AllowAnonymous]
        public ActionResult Login()
        {
            //如果用户记住密码
            
            if (this.User != null && this.User.Identity.IsAuthenticated)
            {
                return RedirectToAction("Index", "Home");
            }
            return View();
        }

        /// <summary>
        /// 验证登录
        /// </summary>
        /// <param name="admininfo"></param>
        /// <returns></returns>
        [AllowAnonymous]

        public JsonResult UserLogin(Admininfo admininfo)
        {
            admininfo.AdminPwd = MD5Encrypt.Encrypt(admininfo.AdminPwd);
            ResponseResult<Admininfo> result = _userService.Login(admininfo);
            //如果登录成功则记住密码
            if (result.success)
            {
                //获取业务层返回的用户信息对象
                Admininfo admin = result.data;
                //实例化用户登录信息
                UserData userData = new Models.UserData();
                userData.AdminId = admininfo.AdminId;
                userData.AdminName = admininfo.AdminName;
                //记住密码
                CookieUtils.SaveLoginCookieInfo(userData);
                //移除原来授权cookie
                CookieUtils.RemoveAuthCookie(this.User);
                CookieUtils.SetAuthCookie(admininfo.AdminName,userData,true);

                AdminUserContext.context.UserInfo_no = admin;                
            }
            return Json(result);
        }

    }
}